<template>
    <div ref="brandRef" class="brand-container" :class="{ 'is-loading': isImageLoading }"
        :style="{ backgroundImage: coverUrl ? `url(${coverUrl})` : 'none' }">
        <div class="brand-mask"></div>
        <div class="brand">
            <h1 class="artboard" :data-text="title">{{ title }}</h1>
        </div>
        <Waves class="hero-waves" />
        <i class="fas fa-chevron-down arrow-down" @click="scrollDown"></i>
    </div>
</template>

<script>
import Waves from "@/components/Waves/index.vue";

export default {
    name: "ArticleCover",
    components: {
        Waves,
    },
    props: {
        coverUrl: {
            type: String,
            default: '',
        },
        title: {
            type: String,
            default: '',
        }
    },
    data() {
        return {
            isImageLoading: false,
        };
    },
    watch: {
        coverUrl: {
            immediate: true,
            handler(newVal) {
                if (newVal) {
                    this.loadBackgroundImage(newVal);
                }
            }
        }
    },
    methods: {
        scrollDown() {
            const mainContent = document.querySelector(".article-main");
            if (mainContent) {
                mainContent.scrollIntoView({ behavior: "smooth" });
            }
        },
        loadBackgroundImage(url) {
            this.isImageLoading = true;
            const img = new Image();
            img.onload = () => {
                setTimeout(() => {
                    this.isImageLoading = false;
                }, 300);
            };
            img.onerror = () => {
                this.isImageLoading = false;
                console.error('Failed to load cover image');
            };
            img.src = url;
        }
    }
};
</script>

<style lang="scss" scoped>
.brand-container {
    height: 55vh;
    position: relative;
    color: #fff;
    background-position: center;
    background-size: cover;
    transition: background-image 1.5s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    overflow: hidden;
    margin-top: -72px;
    padding-top: 80px;
    z-index: 1;

    &:not(.is-loading) {
        background-size: 110%;
        animation: kenburns 45s ease-in-out infinite alternate;
    }
}

@keyframes kenburns {
    0% {
        background-size: 100%;
        background-position: 50% 50%;
    }

    100% {
        background-size: 120%;
        background-position: 30% 70%;
    }
}

.brand-mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(to bottom,
            rgba(0, 0, 0, 0.3) 0%,
            rgba(0, 0, 0, 0.2) 30%,
            rgba(0, 0, 0, 0.2) 70%,
            rgba(0, 0, 0, 0.6) 100%);
    z-index: 0;
}

.brand {
    position: relative;
    z-index: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 0 20px;
    transform: translateY(-30px);
    animation: float 6s ease-in-out infinite;
    width: auto;
    max-width: 90%;
}

@keyframes float {

    0%,
    100% {
        transform: translateY(-30px);
    }

    50% {
        transform: translateY(-40px);
    }
}

.artboard {
    font-size: 2.5rem;
    line-height: 1.4;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2),
        0 0 25px rgba(137, 247, 254, 0.3),
        0 0 35px rgba(102, 166, 255, 0.2);
    font-weight: 800;
    letter-spacing: 2px;
    position: relative;
    padding: 0 20px;
    max-width: 100%;
    overflow: visible;
    white-space: normal;
    z-index: 5;
    display: block;
    -webkit-font-smoothing: antialiased;
    will-change: transform, opacity;
    transition: all 0.5s ease;
    cursor: default;
    background: linear-gradient(135deg, #89f7fe 0%, #66a6ff 50%, #ff9a8b 100%);
    background-size: 300% 300%;
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    animation: shimmer 6s ease-in-out infinite;
    transform: scale(0.95) perspective(1000px) rotateX(2deg);
    transform-origin: center;
}

.artboard:hover {
    transform: scale(1) perspective(1000px) rotateX(0deg);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.25),
        0 0 30px rgba(137, 247, 254, 0.5),
        0 0 45px rgba(102, 166, 255, 0.4);
    letter-spacing: 3px;
}

@keyframes shimmer {
    0% {
        background-position: 0% 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0% 50%;
    }
}

/* 添加标题特效 */
.artboard::after {
    content: attr(data-text);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: -2;
    background: radial-gradient(ellipse at center, rgba(255, 255, 255, 0.5) 0%, transparent 70%);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    filter: blur(20px);
    opacity: 0;
    animation: glow-pulse 4s ease-in-out infinite alternate;
}

@keyframes glow-pulse {
    from {
        opacity: 0.6;
        filter: blur(15px);
    }

    to {
        opacity: 1;
        filter: blur(25px) brightness(1.5);
    }
}

.hero-waves {
    position: absolute;
    bottom: -1px;
    left: 0;
    width: 100%;
    z-index: 1;
    pointer-events: none;
    filter: drop-shadow(0px -5px 10px rgba(0, 0, 0, 0.1));
}

.arrow-down {
    position: absolute;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    cursor: pointer;
    animation: bounce 2s infinite;
    z-index: 2;
    color: white;
    font-size: 32px;
    filter: drop-shadow(0 2px 5px rgba(0, 0, 0, 0.5));
    transition: all 0.3s ease;
    background: rgba(0, 0, 0, 0.2);
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
}

.arrow-down:hover {
    color: #e6e6ff;
    filter: drop-shadow(0 0 15px rgba(255, 255, 255, 0.7));
    transform: translateX(-50%) scale(1.2);
    background: rgba(0, 0, 0, 0.3);
}

@keyframes bounce {

    0%,
    20%,
    50%,
    80%,
    100% {
        transform: translate(-50%, 0);
    }

    40% {
        transform: translate(-50%, -20px);
    }

    60% {
        transform: translate(-50%, -10px);
    }
}

@media (max-width: 768px) {
    .artboard {
        font-size: 2.2rem;
        letter-spacing: 1px;
    }

    .artboard:hover {
        transform: scale(0.95) perspective(1000px) rotateX(0deg);
    }

    .brand-container {
        height: 45vh;
        padding-top: 64px;

        &:not(.is-loading) {
            animation: kenburns-mobile 45s ease-in-out infinite alternate;
        }
    }

    .brand {
        margin-top: -10px;
        transform: translateY(0);
        animation: float-mobile 6s ease-in-out infinite;
        max-width: 95%;
    }

    @keyframes float-mobile {

        0%,
        100% {
            transform: translateY(0);
        }

        50% {
            transform: translateY(-10px);
        }
    }

    .brand::before {
        width: 105%;
        height: 110%;
    }

}

@media (max-width: 480px) {
    .artboard {
        font-size: 2rem;
        letter-spacing: 0.5px;
    }

    .artboard:hover {
        transform: scale(0.9) perspective(1000px) rotateX(0deg);
    }

    .brand-container {
        height: 40vh;
        padding-top: 60px;

        &:not(.is-loading) {
            animation: none;
            background-size: cover;
        }
    }

    .brand::before {
        width: 100%;
    }

    .arrow-down {
        bottom: 20px;
        font-size: 24px;
    }

    .hero-waves {
        height: 60px;
    }

    .subtitle-container {
        margin-top: 12px;
        max-width: 95%;
        padding: 6px 12px;
        font-size: 0.85rem;
    }
}

@keyframes kenburns-mobile {
    0% {
        background-size: 120%;
        background-position: 50% 50%;
    }

    100% {
        background-size: 140%;
        background-position: 40% 60%;
    }
}

/* 添加动态效果 */
.brand-container.is-loading {
    background: linear-gradient(-60deg,
            #0b0217,
            #0e0524,
            #1d0c3d,
            #3c165a,
            #1d0c3d,
            #0e0524,
            #0b0217);
    background-size: 300% 300%;
    animation: gradient-flow 8s ease-in-out infinite;
}

@keyframes gradient-flow {
    0% {
        background-position: 0% 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0% 50%;
    }
}

/* 文章封面特有的粒子效果 */
.brand::before {
    content: '';
    position: absolute;
    width: 110%;
    height: 120%;
    top: -10%;
    left: -5%;
    background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.1) 0%, rgba(0, 0, 0, 0.4) 70%);
    border-radius: 50%;
    opacity: 0.7;
    z-index: -1;
}

.subtitle-container {
    margin-top: 20px;
    background: rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    border-radius: 12px;
    padding: 10px 20px;
    max-width: 80%;
    display: inline-block;
    color: rgba(255, 255, 255, 0.9);
    font-weight: 500;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 1rem;
}
</style>